﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Presenter_SSResourceManager;
using Model_SSResourceManager;
using System.Drawing;

namespace WebView_SSResourceManager.UserPages
{
    public partial class EditProfile : System.Web.UI.Page, IViewEditProfile
    {
        PresenterEditProfile _presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new PresenterEditProfile(this);
            if (!IsPostBack)
            {
                LoadUser();
            }
        }

        protected void Submit_OnClick(object sender, EventArgs e)
        {
            _presenter.Update();
        }

        protected void btnChangePassword_OnClick(object sender, EventArgs e)
        {
            _presenter.ChangePassword();
        }
        
        private void LoadUser()
        {
            try
            {
                _presenter.FillFieldToEdit();
            }
            catch (Exception)
            {
                Response.Redirect("~/");
            }
        }

        public Guid GetUserId()
        {
            return Guid.Parse(Server.HtmlEncode(Request.Cookies["UID"].Value));
        }

        public void FillPageFields(User user)
        {
            lblLoginText.Text = user.Login;
            txtFirstName.Text = user.FirstName;
            txtLastName.Text = user.LastName;
            txtEmail.Text = user.Email;
            txtSkype.Text = user.Skype;
            txtMessageTimeSpan.Text = user.MessageTimeSpan.ToString();
        }

        public User GatherPageFieldsInformation(User user)
        {
            user.Login = lblLoginText.Text;
            user.FirstName = txtFirstName.Text;
            user.LastName = txtLastName.Text;
            user.Email = txtEmail.Text;
            user.Skype = txtSkype.Text;
            user.MessageTimeSpan = TimeSpan.Parse(txtMessageTimeSpan.Text);
            return user;
        }

        public int GetPassword()
        {
            return txtOldPassword.Text.GetHashCode();
        }

        public void ResultOfChangePassword(bool resultValue)
        {
            if(resultValue)
            {
                lblResultCangePassword.Text = "Complete!:)";
                lblResultCangePassword.ForeColor = Color.Green;
            }
            else
            {
                lblResultCangePassword.Text = "Please check the entered data";
                lblResultCangePassword.ForeColor = Color.Red;                
            }
        }

        public int GetNewPassword()
        {
            return txtPassword.Text.GetHashCode();
        }
    }
}